//
//  InsureView.m
//  Project two framework
//
//  Created by 张峥 on 16/7/25.
//  Copyright © 2016年 张峥. All rights reserved.
//



#import "InsureView.h"
#import "ZPZNetRequest+InsureList.h"
#import "FundCollectionViewCell.h"
#import "InsureCollectionViewCell.h"
#import "MJRefresh.h"

@interface InsureView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIScrollView          *scrollView;
@property (nonatomic, strong) UICollectionView      *collectView;
@property (nonatomic, strong) NSMutableArray        *dataSource;
@end


@implementation InsureView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self stepUI];
    }
    return self;
}

- (void)stepUI{
    [self loadDataSource];
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.collectView];
}

- (void)loadDataSource{
    [ZPZNetRequest getInsureListPageNum:@"1" pageSize:@"10" UserId:@"22" comletion:^(id object, BOOL success) {
        if (success) {
            _dataSource =[NSMutableArray arrayWithArray:object[@"content"]];
        }
    }];
}


- (void)refreshHeader{
    [ZPZNetRequest getInsureListPageNum:@"1" pageSize:@"10" UserId:@"22" comletion:^(id object, BOOL success) {
        if (success) {
            _dataSource =[NSMutableArray arrayWithArray:object[@"content"]];
            [_collectView reloadData];
            [_scrollView.mj_header endRefreshing];
        }
    }];

}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(0, self.bounds.size.height*1.5);
        _scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self refreshHeader];
        }];
    }
    return _scrollView;
}


- (UICollectionView *)collectView{
    if (!_collectView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(275, 275);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 20;
        
        //设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.frame), 300) collectionViewLayout:layout];
        _collectView.backgroundColor = [UIColor whiteColor];
        _collectView.showsHorizontalScrollIndicator = NO;
        _collectView.delegate = self;
        _collectView.contentInset = UIEdgeInsetsMake(10, 20, 0, 0);
        _collectView.pagingEnabled = YES;
        _collectView.dataSource = self;
        [_collectView registerNib:[UINib nibWithNibName:@"InsureCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"FundView"];
    }
    return _collectView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    InsureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FundView" forIndexPath:indexPath];
    NSDictionary *dic = _dataSource[indexPath.row];
    cell.titleLabel.text = [NSString stringWithFormat:@"【%@】",dic[@"number"]];
    cell.titleOne.text =  dic[@"characteristic"];
    cell.titleTwo.text = [NSString stringWithFormat:@"产品属性:%@",dic[@"functions"] ];
    cell.titleThree.text = [NSString stringWithFormat:@"发行公司:%@",dic[@"company"]];
    NSNumber *number = dic[@"crowd_age"];
    NSString *string = [NSString stringWithFormat:@"%@",number];
    cell.titleFour.text = [NSString stringWithFormat:@"适用人群:%@岁客户",string];
    NSNumber *number1 = dic[@"times"];
    NSString *string1 = [NSString stringWithFormat:@"保险期限:%@年",number1];
    cell.titleFive.text = [NSString stringWithFormat:@"%@",string1];
    return cell;
}




@end
